﻿// Page1.cpp: 实现文件
//

#include "pch.h"
#include "wowlua02.h"
#include "afxdialogex.h"
#include "Page1.h"
#include "mgr.h"
#include "lua_register.h"
#include "cpplib.h"


// Page1 对话框

IMPLEMENT_DYNAMIC(Page1, CDialogEx)

Page1::Page1(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_PAGE1, pParent)
	, p1luastring_(_T("")) {

}

Page1::~Page1()
{
}

void Page1::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, p1luastring_);
}


BEGIN_MESSAGE_MAP(Page1, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON1, &Page1::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &Page1::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &Page1::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &Page1::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON5, &Page1::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON6, &Page1::OnBnClickedButton6)
END_MESSAGE_MAP()

// Page1 消息处理程序

// 执行脚本
void Page1::OnBnClickedButton1() {
	UpdateData(TRUE);
	std::string luastr = p1luastring_.GetBuffer();
	Mgr::get()->engine()->dostringex(atou8(luastr).c_str());
}

// 执行脚本文件
void Page1::OnBnClickedButton2() {
	// 预加载部分
	//const std::vector<std::string> preloads{
 //       "C:\\Inject\\wow\\wow3.3.5\\test\\common.lua"
	//};
	//for (auto& preload : preloads) {
	//	Mgr::get()->engine()->dofile(preload);
	//}

	const auto fp = "C:\\Inject\\wow\\wow3.3.5\\test\\wow.lua";
	Mgr::get()->engine()->dofile(fp);
}

// Bypass check
void Page1::OnBnClickedButton3() {
	dbgprint("codergege 注册LUA接口开始 [%s][%d]", __FUNCTION__, __LINE__);
	//uint64 check_addr = Mgr::get()->module_base() + RVA_REG_CHECK_START_BASE;
	//ULONG old;
	// nt_protect_virtual_memory(GetCurrentProcess(), (PVOID*)check_addr, (PULONG)0x100, PAGE_READWRITE, &old);
	// BOOL result = VirtualProtect((LPVOID)check_addr, (uint64)0x100, PAGE_EXECUTE_READWRITE, &old);
	Mgr::get()->engine()->bypass_lua_reg_checking();
	lua_reg();
	//nt_protect_virtual_memory(GetCurrentProcess(), (PVOID*)check_addr, (PULONG)0x100, old, &old);

	Mgr::get()->engine()->bypass_lua_invoke_checking();
	dbgprint("codergege 注册LUA接口结束 [%s][%d]", __FUNCTION__, __LINE__);

	dbgprint("codergege START = %llX", Mgr::get()->engine()->bypass_check_start());
	dbgprint("codergege END = %llX", Mgr::get()->engine()->bypass_check_end());
}

// 测试
void Page1::OnBnClickedButton4() {
	// 测试从 lua 注册函数获取返回值
	auto r1 = Mgr::get()->engine()->get_container_num_slots(0);
	auto r2 = Mgr::get()->engine()->get_container_num_slots(1);
	dbgprint("codergege r1=%d, r2=%d", r1, r2);
}


void Page1::OnBnClickedButton5() {
	driver_on();
}


void Page1::OnBnClickedButton6() {
	driver_off();
}
